﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    internal class SiteColumnListReferences : ObjectReferenceList
    {
        public SiteColumnListReferences(LicensingMode licensingMode)
            : base(licensingMode)
        {
        }

        public override ObjectReferenceType ObjectReferenceType
        {
            get { return ObjectReferenceType.ContentTypeList; }
        }

        internal void Add(SPList list, SiteObjectList objects, SPWeb web, SPSite site, SPWebApplication webApplication)
        {
            foreach (SPField field in list.Fields)
            {
                if (SiteColumnList.IsSiteColumn(web, field))
                {
                    SiteObject siteObject = objects.GetItemById(field.Id.ToString("B"));
                    if (siteObject != null && siteObject.Index)
                    {
                        if (Globals.MatchingScope(siteObject, webApplication, site, web))
                        {
                            string refId = Globals.GetReferenceId(siteObject, field); 
                            ObjectReference newReference = new ObjectReference(list.ID.ToString("B"), list.Title, refId, ObjectReferenceType.SiteColumnList);
                            newReference.Url = string.Format("{0}/_layouts/FldEdit.aspx?List={1}&Field={2}", web.Url, System.Web.HttpUtility.UrlEncode(list.ID.ToString()), field.InternalName);
                            newReference.SiteTitle = web.Title;
                            newReference.SiteUrl = web.Url;
                            newReference.ListTitle = list.Title;
                            newReference.ListUrl = site.MakeFullUrl(list.DefaultViewUrl);
                            Add(newReference);
                            if (siteObject.IndexItems)
                            {
                                AddItemReferences(field, siteObject, list, site, web, refId);
                            }
                        }
                    }
                }
            }
        }

        private void AddItemReferences(SPField field, SiteObject siteObject, SPList list, SPSite site, SPWeb web, string refId)
        {
            foreach (SPListItem item in list.Items)
            {
                if (item[field.Id] != null)
                {
                    ObjectReference newReference = new ObjectReference(item.UniqueId.ToString("B"), Globals.GetItemTitle(item), refId, ObjectReferenceType.SiteColumnListItem);
                    newReference.Url = Globals.GetItemUrl(site.MakeFullUrl(list.DefaultViewUrl), item, site.MakeFullUrl(item.Url));
                    newReference.SiteTitle = web.Title;
                    newReference.SiteUrl = web.Url;
                    newReference.ListTitle = list.Title;
                    newReference.ListUrl = site.MakeFullUrl(list.DefaultViewUrl);
                    Add(newReference);
                }
            }
        }
    }
}
